package com.fenghongzhang.day008view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

public class MyView extends FrameLayout {

    LayoutParams refreshParams;
    LinearLayout linearLayout;
    PointF pointF = new PointF();
    public MyView(Context context) {
        super(context);
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        init();
    }

    private void init() {
//        LinearLayout linearLayout = new LinearLayout(getContext());
//        LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 200);
//        layoutParams.topMargin = 200;
//        linearLayout.setLayoutParams(layoutParams);
//        linearLayout.setBackgroundColor(Color.RED);
//
//        addView(linearLayout);

         linearLayout = new LinearLayout(getContext());
         refreshParams = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, 200);
        refreshParams.topMargin = -200;
        linearLayout.setLayoutParams(refreshParams);
        linearLayout.setBackgroundColor(Color.RED);

        addView(linearLayout);

    }


    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            pointF.x = ev.getX();
            pointF.y = ev.getY();
        } else if (ev.getAction() == MotionEvent.ACTION_MOVE) {
            //偏移量
            float y = Math.abs(ev.getY() - pointF.y);
            float x = Math.abs(ev.getX() - pointF.x);
            //判读滑动方向
            if(y < 5){
                return super.onTouchEvent(ev);
            }
            if(x > y *2 ){
                return super.onTouchEvent(ev);
            }
            //最小响应距离

            FrameLayout.LayoutParams layoutParams = (LayoutParams) linearLayout.getLayoutParams();

            layoutParams.topMargin += ev.getY() - pointF.y;

            if(layoutParams.topMargin > 0 ){
                layoutParams.topMargin = 0;
            }

            if(layoutParams.topMargin < -200){
                layoutParams.topMargin = -200;
            }


            linearLayout.setLayoutParams(layoutParams);




            //可滑动范围
            //Main下拉
            pointF.x = ev.getX();
            pointF.y = ev.getY();

        } else if (ev.getAction() == MotionEvent.ACTION_UP) {
            //当滑动位置，打开或关闭
            //当滑动位置，打开或关闭
            LayoutParams mainParams = (LayoutParams) linearLayout.getLayoutParams();
            View view = getChildAt(1);

            if (mainParams.topMargin < -200 / 2 ) {
                startAnim(mainParams.topMargin, -200);
                view.setVisibility(INVISIBLE);

            } else {
                startAnim(mainParams.topMargin, 0);
                view.setVisibility(VISIBLE);


            }

        }

        return true;
    }

    private void startAnim(int start, int end) {
        ValueAnimator valueAnimator = ValueAnimator.ofInt(start, end);
        valueAnimator.setDuration(500);
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                FrameLayout.LayoutParams mainParams = (LayoutParams) linearLayout.getLayoutParams();
                mainParams.topMargin = (int) animation.getAnimatedValue();
                linearLayout.setLayoutParams(mainParams);
//                requestLayout();

            }
        });
        valueAnimator.start();
    }
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        View view = getChildAt(1);
//        view.setBackgroundColor(Color.BLUE);
        view.layout(this.getWidth()/2-100,200/2,this.getWidth()/2+100,200/2+100);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        for (int i = 0; i < getChildCount(); i++) {
            View view = getChildAt(i);
//            measureChild(view, widthMeasureSpec, heightMeasureSpec);
        }

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        canvas.drawColor(Color.BLUE);
    }


}
